package com.zelin.controller;

import com.zelin.mapper.StudentMapper;
import com.zelin.pojo.PageUtils;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.pojo.vo.StudentVo;
import com.zelin.cart.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/7/23-14:31
 * ------------------------------
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;
    //1.列表
    @GetMapping("list")
    public R findAll(){
        List<Student> students = studentMapper.selectList(null);
        return R.ok().put("students",students);
    }

    //2.插入
    @PostMapping("add")
    public R add(@RequestBody Student student){
        studentMapper.insert(student);
        return R.ok().put("msg","添加学生成功！");
    }

    //3. 修改
    @PutMapping("update/{sid}")
    public R update(@PathVariable long sid,@RequestBody Student student){
        student.setSid(sid);
        student.setVersion(1);
        studentMapper.updateById(student);
        return R.ok().put("msg","修改学生成功！");
    }

    //4. 根据查询关键字进行查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@RequestBody StudentVo studentVo,@PathVariable  Integer page,@PathVariable  Integer pageSize){
        PageUtils pageUtils = studentService.search(page,pageSize,studentVo);
        return R.ok().put("page",pageUtils);
    }

    //5. 删除学生
    @DeleteMapping("/delete/{sid}")
    public R delete(@PathVariable Long sid){
        studentService.delete(sid);
        return R.ok().put("msg","删除成功！");
    }


}
